<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="分类名" prop="classifyName">
        <el-input v-model="queryParams.classifyName" placeholder="请输入分类名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['classify:classifyMain:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['classify:classifyMain:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['classify:classifyMain:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['classify:classifyMain:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="classifyMainList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="分类id" align="center" prop="id" />
      <el-table-column label="分类名" align="center" prop="classifyName">
        <template slot-scope="scope">
          <el-link @click="handleClassify(scope.row)">{{ scope.row.classifyName }}</el-link>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['classify:classifyMain:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['classify:classifyMain:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" 
      :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改商品分类对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="分类名" prop="classifyName">
          <el-input v-model="form.classifyName" placeholder="请输入分类名" />
        </el-form-item>
        <el-form-item label="分类图片" prop="classifyImg">
          <image-upload v-model="form.classifyImg" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="titleClassify" :visible.sync="openClassify" width="500px">
      <el-form :model="classifyTwoParams" ref="queryChild" size="small" :inline="true" label-width="68px">
        <el-form-item label="子分类名" prop="classifytwoName">
          <el-input
            v-model="classifyTwoParams.classifytwoName"
            placeholder="请输入子分类名"
            clearable
            @keyup.enter.native="searchClassifyTwo"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="searchClassifyTwo">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetChildQuery">重置</el-button>
        </el-form-item>
    </el-form>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
            <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="addChild"
          >新增</el-button>
        </el-col>
      </el-row>
      
        <!-- v-hasPermi="['classify:classifyTwo:add']" -->
      <el-table v-loading="loading2" :data="classifyTwoList">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="子分类id" align="center" prop="id" />
        <el-table-column label="子分类名" align="center" prop="classifytwoName" />
        <el-table-column label="子分类图片" align="center" prop="classifytwoImg" width="100">
          <template slot-scope="scope">
            <image-preview :src="scope.row.classifytwoImg" :width="50" :height="50" />
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-edit" @click="updateChild(scope.row)">修改</el-button>
              <!-- v-hasPermi="['classify:classifyTwo:edit']" -->
            <el-button size="mini" type="text" icon="el-icon-delete" @click="deleteChild(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
        <pagination
        v-show="classifyTwoTotal>0"
        :total="classifyTwoTotal"
        :page.sync="classifyTwoParams.pageNum"
        :limit.sync="classifyTwoParams.pageSize"
        @pagination="initClassifyTwo"
      />
    </el-dialog>

    <!-- 添加或修改子菜单对话框 -->
    <el-dialog :title="titleChild" :visible.sync="openChild" width="500px">
      <el-form ref="childForm" :model="childForm" :rules="childFormRules" label-width="80px">
        <el-form-item label="菜单名" prop="classifytwoName">
          <el-input v-model="childForm.classifytwoName" placeholder="请输入子菜单名" />
        </el-form-item>
        <el-form-item label="菜单图片" prop="classifytwoImg">
          <image-upload v-model="childForm.classifytwoImg" :limit="1"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitChild">确 定</el-button>
        <el-button @click="cancelChild">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listClassifyMain, getClassifyMain, delClassifyMain, addClassifyMain, updateClassifyMain } from "@/api/classify/classifyMain";
import { listClassifyTwo, getClassifyTwo, delClassifyTwo, addClassifyTwo, updateClassifyTwo } from "@/api/classify/classifyTwo";

export default {
  name: "ClassifyMain",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品分类表格数据
      classifyMainList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        classifyName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      openClassify:false,
      titleClassify:"",
      loading2:true,
      classifyTwoParams:{
        pageNum: 1,
        pageSize: 10,
        classifyId:null,
        classifytwoName: null
      },
      classifyTwoTotal:0,
      //子商品分类数据
      classifyTwoList:[],
      titleChild:"",
      openChild:false,
      childForm:{
      },
      childFormRules:{
        classifytwoName: [
          { required: true, message: "请输入子分类名", trigger: "blur" }
        ],
        classifytwoImg:[
          { required: true, message: "请上传子分类图片", trigger: "change" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /**
     * 重置子分类查询
     */
    resetChildQuery(){
      this.resetForm("queryChild");
      this.searchClassifyTwo()
    },
    /**
     * 查询子分类
     */
    searchClassifyTwo(){
      this.classifyTwoParams.pageNum = 1;
      this.initClassifyTwo()
    },
    /**
     * 删除子分类
     * @param row 子分类数据
     */
    deleteChild(row){
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除子分类编号为"' + ids + '"的数据项？').then(function() {
        return delClassifyTwo(ids);
      }).then(() => {
        this.initClassifyTwo();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },

    /**
     * 打开子分类（修改）
     * @param row 子分类数据
     */
    updateChild(row){
      this.resetChildForm()
      const id = row.id || this.ids
      getClassifyTwo(id).then(response => {
        this.childForm = response.data;
        this.openChild = true;
        this.titleChild = "修改子菜单";
      });
    },
    /**
     * 打开子分类（新增）
     */
    addChild(){
      this.resetChildForm()
      this.titleChild = "添加子菜单"
      this.openChild = true;
    },
    cancelChild(){
      this.openChild = false;
      this.resetChildForm()
    },


    /**
     * 修改/新增子分类
     */
    submitChild(){
      this.$refs["childForm"].validate(valid => {
        if (valid) {
          if (this.childForm.id != null) {
            updateClassifyTwo(this.childForm).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.openChild = false;
              this.initClassifyTwo();
            });
          } else {
            addClassifyTwo(this.childForm).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.openChild = false;
              this.initClassifyTwo();
            });
          }
        }
      });
    },

    /**
     * 重置子分类(修改/新增)表单
     */
    resetChildForm(){
      // this.childForm.classifytwoName=null;
      // this.childForm.classifytwoImg=null;
      this.resetForm("childForm");
    },

    /**
     * 初始化子分类列表
     */
    initClassifyTwo(){
      this.loading2 = true;
      listClassifyTwo(this.classifyTwoParams).then(response => {
        this.classifyTwoList = response.rows;
        this.classifyTwoTotal = response.total;
        this.loading2 = false;
      });
    },
    /**
     * 打开主分类菜单
     * @param row 商品分类数据
     */
    handleClassify(row){
      this.resetForm("queryChild");
      this.classifyTwoParams.classifyId = row.id;
      this.childForm.classifyId =  row.id
      this.initClassifyTwo();
      this.titleClassify = row.classifyName + " 子级分类管理"
      this.openClassify = true;
    },
    /** 查询商品分类列表 */
    getList() {
      this.loading = true;
      listClassifyMain(this.queryParams).then(response => {
        this.classifyMainList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        classifyName: null,
        classifyImg: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商品分类";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getClassifyMain(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改商品分类";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateClassifyMain(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addClassifyMain(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除商品分类编号为"' + ids + '"的数据项？').then(function() {
        return delClassifyMain(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('classify/classifyMain/export', {
        ...this.queryParams
      }, `classifyMain_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
